package 二叉树;

public class Solution9 {
    //https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
    int prev = 0;
    int flag = 0;
    public boolean isValidBST (TreeNode root) {
        if (root == null) {
            return true;
        }
        if (!isValidBST(root.left)) {
            return false;
        }
        if (flag == 0) {
            flag = 1;
            prev = root.val;
            return true;
        }
        if (root.val <= prev) {
            return false;
        }
        prev = root.val;
        if (!isValidBST(root.right)) {
            return false;
        }
        return true;
    }
}
